home *** CD-ROM | disk | FTP | other *** search
/ Ian & Stuart's Australian Mac 1993 September / September 93.iso / Archives / Utilities / System / FKey / FKeys / dcl-ANSI (all) / dcl-ANSI Extension.c < prev    next >
C/C++ Source or Header  |  1993-04-15  |  8KB  |  384 lines

  1. /*
  2.  * dcl-ANSI Extension.c
  3.  *
  4.  * By Jamie McCarthy, April 92.  This is public domain.
  5.  *
  6.  * This source file provides a bridge between dcl-ANSI.c,
  7.  * which just does a translation, and BBEdit.
  8.  *
  9.  */
  10.  
  11.  
  12.  
  13. /******************************/
  14.  
  15. #include <SetupA4.h>
  16. #include <pascal.h>
  17. #include <ExternalInterface.h>
  18. #include <DialogUtilities.h>
  19.  
  20. #include "dcl-ANSI.h"
  21.  
  22. /******************************/
  23.  
  24.     /*
  25.      * The DITL item numbers for the various controls.
  26.      */
  27.  
  28. enum {
  29.     kShowWhenOptionKeyDown = 3,
  30.     kShowWhenOptionKeyUp,
  31.     
  32.     kLookInClipboard,
  33.     kLookInSelection,
  34.     
  35.     kPutToClipboard,
  36.     kPutToSelection,
  37.     kPutToDialog,
  38.     
  39.     kLine1,
  40.     kLine2
  41. } ;
  42.  
  43. /******************************/
  44.  
  45.     /*
  46.      * Parameters passed to main().
  47.      */
  48. ExternalCallbackBlock *gCallbacks;
  49. WindowPtr gWindPtr;
  50.  
  51.     /*
  52.      * The preferences.
  53.      */
  54. struct {
  55.     short show;
  56.     short look;
  57.     short put;
  58. } gOptionsDCL;    
  59.  
  60.     /*
  61.      * Do we have a window?
  62.      * Was something selected when we started?
  63.      */
  64. Boolean gWeHaveAWindow;
  65. Boolean gNonEmptySelection;
  66.  
  67.     /*
  68.      * Information about the selection.
  69.      */
  70. long gSelStart, gSelEnd, gFirstChar;
  71.  
  72.     /*
  73.      * C string data.
  74.      */
  75. Handle gCStrList128=NULL, gCStrList129=NULL;
  76.  
  77.     /*
  78.      * The text we pass to ParamText.
  79.      */
  80. Str255 gFinalStr[4];
  81.  
  82. /******************************/
  83.  
  84. pascal void main(ExternalCallbackBlock *callbacks, WindowPtr w);
  85.  
  86. void maintainButtons(DialogPtr d);
  87. void getCDeclaration(void);
  88. void putFinalString(void);
  89.  
  90. void startupCStrings(void);
  91. void shutdownCStrings(void);
  92.  
  93. /******************************/
  94.  
  95.  
  96.  
  97. pascal void main(ExternalCallbackBlock *callbacks, WindowPtr w)
  98. {
  99.     DialogPtr d;
  100.     short item;
  101.     short    actualPrefsLen;
  102.     KeyMap theKeys;
  103.     Boolean showPrefsDialog;
  104.     
  105.     RememberA0();
  106.     SetUpA4();
  107.     
  108.     gCallbacks = callbacks;
  109.     gWindPtr = w;
  110.     gError = noErr;
  111.     
  112.     gWeHaveAWindow = (w != NULL);
  113.     if (gWeHaveAWindow) {
  114.         callbacks->GetSelection(&gSelStart, &gSelEnd, &gFirstChar);
  115.         gNonEmptySelection = (gSelEnd > gSelStart);
  116.     }
  117.     
  118.     callbacks->GetPreference('dcl!', sizeof(gOptionsDCL), &gOptionsDCL, &actualPrefsLen);
  119.     if (actualPrefsLen < sizeof(gOptionsDCL)) {
  120.         gOptionsDCL.show = kShowWhenOptionKeyUp;
  121.         gOptionsDCL.look = kLookInClipboard;
  122.         gOptionsDCL.put = kPutToDialog;
  123.         showPrefsDialog = TRUE;
  124.     } else {
  125.         GetKeys(theKeys);
  126.         showPrefsDialog =
  127.             ( ((theKeys[1] & 0x04) != 0) ^ (gOptionsDCL.show == kShowWhenOptionKeyUp) );
  128.     }
  129.     
  130.     if (showPrefsDialog) {
  131.         GrafPtr oldPort;
  132.         GetPort(&oldPort);
  133.         d = callbacks->CenterDialog(128);
  134.         SetPort(d);
  135.         
  136.         SetupUserItem(d, kLine1, callbacks->FrameDialogItem);
  137.         SetupUserItem(d, kLine2, callbacks->FrameDialogItem);
  138.         
  139.         do {
  140.             maintainButtons(d);
  141.             ModalDialog(callbacks->StandardFilter, &item);
  142.             if (item <= kShowWhenOptionKeyUp && item >= kShowWhenOptionKeyDown) {
  143.                 gOptionsDCL.show = item;
  144.             } else if (item <= kLookInSelection && item >= kLookInClipboard) {
  145.                 gOptionsDCL.look = item;
  146.             } else if (item >= kPutToClipboard && item <= kPutToDialog) {
  147.                 gOptionsDCL.put = item;
  148.             }
  149.         } while (item != ok && item != cancel);
  150.         
  151.         DisposDialog(d);
  152.         SetPort(oldPort);
  153.     } else {
  154.         item = ok;
  155.     }
  156.     
  157.     if (!gNonEmptySelection) {
  158.         gOptionsDCL.look = kLookInClipboard;
  159.     }
  160.     
  161.     if (item == ok) {
  162.         
  163.         if (showPrefsDialog) {
  164.             callbacks->SetPreference('dcl!', sizeof(gOptionsDCL), &gOptionsDCL, &actualPrefsLen);
  165.         }
  166.         
  167.         getCDeclaration();
  168.         
  169.         startupDcl();
  170.         if (gError == noErr) {
  171.             declarator();
  172.         }
  173.         buildFinalString();
  174.         shutdownDcl();
  175.         
  176.         putFinalString();
  177.         
  178.     }
  179.     
  180.     RestoreA4();
  181. }
  182.  
  183.  
  184.  
  185. /******************************/
  186.  
  187.  
  188.  
  189. void maintainButtons(DialogPtr d)
  190. {
  191.         /*
  192.          * If there's no selection, we can't look there for input.
  193.          */
  194.     if (!gNonEmptySelection) {
  195.         XAbleDlgCtl(d, kLookInSelection, FALSE);
  196.         gOptionsDCL.look = kLookInClipboard;
  197.     }
  198.     
  199.     SetDlgCtl(d, kShowWhenOptionKeyDown,    (gOptionsDCL.show == kShowWhenOptionKeyDown));
  200.     SetDlgCtl(d, kShowWhenOptionKeyUp,        (gOptionsDCL.show == kShowWhenOptionKeyUp));
  201.     SetDlgCtl(d, kLookInClipboard,            (gOptionsDCL.look == kLookInClipboard));
  202.     SetDlgCtl(d, kLookInSelection,            (gOptionsDCL.look == kLookInSelection));
  203.     SetDlgCtl(d, kPutToClipboard,                (gOptionsDCL.put == kPutToClipboard));
  204.     SetDlgCtl(d, kPutToSelection,                (gOptionsDCL.put == kPutToSelection));
  205.     SetDlgCtl(d, kPutToDialog,                    (gOptionsDCL.put == kPutToDialog));
  206. }
  207.  
  208.  
  209.  
  210. void getCDeclaration(void)
  211. {
  212.     if (gOptionsDCL.look == kLookInClipboard) {
  213.         long theLength, theOffset;
  214.         gCDeclHndl = NewHandle(0);
  215.         theLength = GetScrap(gCDeclHndl, 'TEXT', &theOffset);
  216.         if (theLength < 0) {
  217.             gError = theLength;
  218.             gCDeclHndl = NULL;
  219.         }
  220.     } else {
  221.         long theLength, theFreeMem;
  222.         char *selStartPtr;
  223.         theLength = gSelEnd - gSelStart;
  224.         theFreeMem = CompactMem(theLength);
  225.         if (theFreeMem < theLength) {
  226.             gError = memFullErr;
  227.             gCDeclHndl = NULL;
  228.         } else {
  229.             gCDeclHndl = NewHandle(theLength);
  230.             selStartPtr = *gCallbacks->GetWindowContents(gWindPtr) + gSelStart;
  231.             BlockMove(selStartPtr, *gCDeclHndl, theLength);
  232.         }
  233.     }
  234. }
  235.  
  236.  
  237.  
  238. void putFinalString(void)
  239. {
  240.     switch (gOptionsDCL.put) {
  241.         
  242.         case kPutToClipboard: {
  243.             long theLength;
  244.             gError = ZeroScrap();
  245.             if (gError == noErr) {
  246.                 theLength = strlen(gEnglish);
  247.                 gError = PutScrap(theLength, 'TEXT', &gEnglish[0]);
  248.             }
  249.         }    break;
  250.             
  251.         case kPutToSelection: {
  252.             Handle theText;
  253.             short englishLen;
  254.             long diffLen;
  255.             long oldTextLen;
  256.             englishLen = strlen(gEnglish);
  257.             diffLen = englishLen+gSelStart-gSelEnd;
  258.             if (!gWeHaveAWindow) {
  259.                 gWindPtr = gCallbacks->NewDocument();
  260.                 gSelStart = gSelEnd = gFirstChar = 0;
  261.             }
  262.             theText = gCallbacks->GetWindowContents(gWindPtr);
  263.             oldTextLen = GetHandleSize(theText);
  264.             if (diffLen > 0) {
  265.                 SetHandleSize(theText, oldTextLen+diffLen);
  266.                 BlockMove(*theText + gSelEnd,
  267.                     *theText + gSelEnd + diffLen,
  268.                     oldTextLen - gSelEnd);
  269.             } else {
  270.                 BlockMove(*theText + gSelEnd,
  271.                     *theText + gSelEnd + diffLen,
  272.                     oldTextLen - gSelEnd);
  273.                 SetHandleSize(theText, oldTextLen+diffLen);
  274.             }
  275.             BlockMove(gEnglish, *theText+gSelStart, englishLen);
  276.             gCallbacks->ContentsChanged(gWindPtr);
  277.             gCallbacks->SetSelection(gSelStart, gSelStart+englishLen, gFirstChar);
  278.         }    break;
  279.             
  280.         case kPutToDialog: {
  281.             short wFinalStr;
  282.             short englishLen, englishOffset;
  283.             GrafPtr oldPort;
  284.             DialogPtr theReportDlg;
  285.             short item;
  286.             
  287.                 /*
  288.                  * Break gEnglish into four Pascal strings and stuff them
  289.                  * into gFinalStr[0-3].
  290.                  */
  291.             
  292.             gFinalStr[0][0] = gFinalStr[1][0] = gFinalStr[2][0] = gFinalStr[3][0] = 0;
  293.             wFinalStr = 0;
  294.             englishLen = strlen(gEnglish);
  295.             englishOffset = 0;
  296.             while (englishLen > 0) {
  297.                 if (englishLen > 239) {
  298.                     gFinalStr[wFinalStr][0] = 240;
  299.                     BlockMove(&gEnglish[englishOffset], &gFinalStr[wFinalStr][1], 240);
  300.                     englishLen -= 240;
  301.                     englishOffset += 240;
  302.                     ++wFinalStr;
  303.                 } else {
  304.                     gFinalStr[wFinalStr][0] = englishLen;
  305.                     BlockMove(&gEnglish[englishOffset], &gFinalStr[wFinalStr][1], englishLen);
  306.                     englishLen = 0;
  307.                 }
  308.             }
  309.             
  310.             GetPort(&oldPort);
  311.             ParamText(gFinalStr[0], gFinalStr[1], gFinalStr[2], gFinalStr[3]);
  312.             theReportDlg = gCallbacks->CenterDialog(129);
  313.             SetPort(theReportDlg);
  314.             do {
  315.                 ModalDialog(gCallbacks->StandardFilter, &item);
  316.             } while (item != ok);
  317.             DisposDialog(theReportDlg);
  318.             SetPort(oldPort);
  319.         }    break;
  320.             
  321.     }
  322. }
  323.  
  324.  
  325.  
  326. /******************************/
  327.  
  328.  
  329.  
  330. void startupCStrs(void)
  331. {
  332.     gCStrList128 = GetResource('CST#', 128);
  333.     if (gCStrList128 == NULL) {
  334.         gError = resNotFound;
  335.             // kind of pointless;  we're going to get a bus error in just a few
  336.             // milliseconds, but oh well...you dink with my resource file, you
  337.             // pay the consequences...
  338.     } else {
  339.         HLock(gCStrList128);
  340.         gCStrList129 = GetResource('CST#', 129);
  341.         if (gCStrList129 == NULL) {
  342.             gError = resNotFound;
  343.         } else {
  344.             HLock(gCStrList129);
  345.         }
  346.     }
  347. }
  348.  
  349.  
  350.  
  351. short getNMiscCStrs(void)
  352. {
  353.     return *(long*)*gCStrList128;
  354. }
  355.  
  356.  
  357.  
  358. char *getMiscCStr(short index)
  359. {
  360.     return *gCStrList128 + ((long*)*gCStrList128)[index];
  361. }
  362.  
  363.  
  364.  
  365. short getNSpecifiers(void)
  366. {
  367.     return *(long*)*gCStrList129;
  368. }
  369.  
  370.  
  371.  
  372. char *getSpecifier(short index)
  373. {
  374.     return *gCStrList129 + ((long*)*gCStrList129)[index];
  375. }
  376.  
  377.  
  378.  
  379. void shutdownCStrs(void)
  380. {
  381.     if (gCStrList128 != NULL) ReleaseResource(gCStrList128);
  382.     if (gCStrList129 != NULL) ReleaseResource(gCStrList129);
  383. }
  384.